import { modalController } from './ModalController';
import { eventBus } from '../../../../eventbus/eventbus';

export default {
  data() {
    return {
      modalController,
    };
  },
  mounted() {
    /**
     * @param modalType
     * 值类型：String：不与其他模态框并存，只显示当前模态框，Array：与其他模态框共存，按层级显示
     */
    eventBus.on('openModal', (modalType, modalParams) => {
      let isArray = Array.isArray(modalType);
      modalType = isArray ? modalType[0] : modalType;

      if (modalController.hasModal(modalType)) {
        return;
      }

      if (!isArray) {
        modalController.clearModalList();
      }

      modalController.addModal({
        modalType: modalType,
        modalParams: modalParams,
      });
    });
  },
  computed: {
    showModal() {
      return function (modalType) {
        return modalController.hasModal(modalType);
      };
    },
    modalParams() {
      return function (type) {
        let {modalType, modalParams} = modalController.getModal(type);
        return {
          modalType,
          modalParams,
        };
      };
    }
  },
  destroyed() {
    eventBus.off('openModal');
    modalController.dispose();
  }
};